package com.kingre.et.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.kingre.et.Constants;

/**
 * 用户
 * 
 * @author Jingui.Yu
 * 
 */
@Entity
@Table(name = "t_user")
public class User extends BaseObject implements UserDetails {
    private static final long serialVersionUID = -544519780852321177L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    // 登录用户名
    @Column(nullable = false, length = 128)
    private String username;

    // 邮件
    @Column(length = 128)
    private String email;

    // 登录密码
    @Column(nullable = false, length = 128)
    private String password;

    // 姓名
    @Column(length = 128)
    private String name;

    // 性别：10=男；20=女
    private Integer sex;

    // 学位:{10=高职;20=本科;30=研究生}
    private Integer degree;

    // 年龄
    private Integer age;

    // 年级
    private Integer grade;

    // 生源
    private Integer source;

    // 学号
    @Column(name = "studentid", length = 56)
    private String studentId;

    // 专业：10=文；20=理；30=工；40=其他
    private Integer major;

    // 高校ID
    @ManyToOne(targetEntity = School.class)
    @JoinColumn(name = "school_id")
    private School school;

    // 0=未激活；1=有效；-1=无效
    @Column(name = "status_code", nullable = false)
    private Integer statusCode;

    @OneToMany(mappedBy = "user", targetEntity = Rawdata.class)
    private Set<Rawdata> rawdata;

    @OneToMany(mappedBy = "user", targetEntity = DimensioScore.class)
    private Set<DimensioScore> dimensioScore;

    // 角色: ADMIN/TEACHER/EXPERT/USER
    @ManyToOne(targetEntity = Role.class)
    @JoinTable(name = "t_user_role_map", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Role role;

    public Integer getId() {
        return id;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public String getName() {
        return name;
    }

    public Integer getSex() {
        return sex;
    }

    public Integer getDegree() {
        return degree;
    }

    public String getStudentId() {
        return studentId;
    }

    public Integer getMajor() {
        return major;
    }

    public String getEmail() {
        return email;
    }

    public School getSchool() {
        return school;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public Set<Rawdata> getRawdata() {
        return rawdata;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.kingre.et.model.BaseObject#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }
        final User user = (User) o;
        return this.hashCode() == user.hashCode();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.kingre.et.model.BaseObject#hashCode()
     */
    @Override
    public int hashCode() {
        return (email != null ? email.hashCode() : 0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.kingre.et.model.BaseObject#toString()
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("email", email)
                .append("password", password).append("name", name).toString();
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public void setDegree(Integer degree) {
        this.degree = degree;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public void setMajor(Integer major) {
        this.major = major;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public void setRawdata(Set<Rawdata> rawdata) {
        this.rawdata = rawdata;
    }

    @Override
    @Transient
    public GrantedAuthority[] getAuthorities() {
        GrantedAuthority[] grantedAuthorities = new GrantedAuthority[1];
        grantedAuthorities[0] = new GrantedAuthorityImpl(role.getName());
        return grantedAuthorities;
    }

    @Override
    @Transient
    public String getUsername() {
        return username;
    }

    @Override
    @Transient
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @Transient
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @Transient
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @Transient
    public boolean isEnabled() {
        if (statusCode == 1)
            return true;
        else
            return false;
    }

    @Transient
    public void addRawdata(Rawdata rawdata) {
        if (this.getRawdata() == null)
            this.setRawdata(new HashSet<Rawdata>());
        this.getRawdata().add(rawdata);
    }

    @Transient
    public boolean isTestFinish() {
        return getRawdata() != null && getRawdata().size() >= Constants.COUNT_QUESTION;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Set<DimensioScore> getDimensioScore() {
        return dimensioScore;
    }

    public void setDimensioScore(Set<DimensioScore> dimensioScore) {
        this.dimensioScore = dimensioScore;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public Integer getSource() {
        return source;
    }

    public void setSource(Integer source) {
        this.source = source;
    }

}
